package com.sunbeam.common.log4j;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;

import com.sunbeam.common.consts.EConstants;
import com.sunbeam.common.utils.StringUtils;

/**
 * 
 * @ClassName: BaseLogger
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2013/09/24 17:57:49
 */
public class BaseLogger extends Logger {

	public BaseLogger(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	// Initializing Logger Parameter
	public static Logger logger = Logger.getLogger(BaseLogger.class);

	/**
	 * 
	 *Title: 
	 *Description:
	 */
//	public BaseLogger() {
//		setBaseLogger(EConstants.DEFAULT_LOG4J_PROPERTIES);
//	}

	/**
	 * 
	 *Title: 
	 *Description: 
	 * @param configFilename
	 */
//	public BaseLogger(String configFilename) {
//		setBaseLogger(configFilename);
//	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// System.out.println("This is println message.");

		// 读取使用Java的特性文件编写的配置文件。
		setBaseLogger("log4j_error.properties");	 
		
		// 记录debug级别的信息
		logger.debug("This is debug message.");
		// 记录info级别的信息
		logger.info("This is info message.");
		// 记录error级别的信息
		logger.error("This is error message.");
	}

	/**
	 * 
	 * @Title: initBaseLogger
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 */
	public static void setBaseLogger(String log4jConfigFileName) {
		setBaseLogger(log4jConfigFileName,System.getProperty("user.dir"));
	}

	/**
	 * 
	 * @Title: setBaseLogger
	 * @Description: TODO
	 * @param @param log4jConfigFileName
	 * @param @param logFilePath
	 * @return void
	 * @throws
	 */
	public static void setBaseLogger(String log4jConfigFileName,String logFilePath) {
		        // Set Log Files Directory
				System.setProperty("LOGDIR", logFilePath);
				
				// File Extension Name
				String configFileExtName=StringUtils.getFileExtName(log4jConfigFileName);
				
				// default Log4J Config
				String configFilename = StringUtils.URLPathCombine(new String[] {
						BaseLogger.class.getClassLoader().getResource(StringUtils.Empty).getPath(),
						EConstants.DEFAULT_SETTINGS_DIRECTORY,
						log4jConfigFileName });
				
				if (EConstants.LOG4J_FILE_EXT_NAME_PROPERTIES.equalsIgnoreCase(configFileExtName)) {
					// Load properties Files
					PropertyConfigurator.configure(configFilename);
				} else if (EConstants.LOG4J_FILE_EXT_NAME_XML.equalsIgnoreCase(configFileExtName)) {
					// Load XML Files
					DOMConfigurator.configure(configFilename);
				} else {
					BasicConfigurator.configure();
				}
				File f=new File("C:\\1.txt");
				 BufferedWriter output;
				try {
					output = new BufferedWriter(new FileWriter(f));
					output.write(BaseLogger.class.getClassLoader().getResource(StringUtils.Empty).getPath());
				    output.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			       
	}
	/**
	*Title: debug
	*Description: 
	* @param message
	* @see org.apache.log4j.Category#debug(java.lang.Object)
	*/
	
	@Override
	public void debug(Object message) {
		// TODO Auto-generated method stub
		super.debug(message);
	}

	/**
	*Title: error
	*Description: 
	* @param message
	* @see org.apache.log4j.Category#error(java.lang.Object)
	*/
	
	@Override
	public void error(Object message) {
		// TODO Auto-generated method stub
		super.error(message);
	}

	/**
	*Title: warn
	*Description: 
	* @param message
	* @see org.apache.log4j.Category#warn(java.lang.Object)
	*/
	
	@Override
	public void warn(Object message) {
		// TODO Auto-generated method stub
		super.warn(message);
	}

	/**
	*Title: info
	*Description: 
	* @param message
	* @see org.apache.log4j.Category#info(java.lang.Object)
	*/
	
	@Override
	public void info(Object message) {
		// TODO Auto-generated method stub
		super.info(message);
	}
	
	

}